/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Test.java
 *
 * Created on Oct 11, 2014, 10:06:46 AM
 */
package copy_ExportToExcel;

import java.awt.print.PrinterException;
import java.io.File;
import java.io.FileOutputStream;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;

/**
 *
 * @author admin
 */
public class Test extends javax.swing.JFrame {

    /** Creates new form Test */
    public Test() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        content_Table = new javax.swing.JTable();
        export_Button = new javax.swing.JButton();
        one_Lable = new javax.swing.JLabel();
        two_Lable = new javax.swing.JLabel();
        three_Lable = new javax.swing.JLabel();
        four_Lable = new javax.swing.JLabel();
        print_Button = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        content_Table.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {"1", "2", "3", "4"},
                {"5", "6", "7", "8"},
                {"9", "10", "11", "12"},
                {"13", "14", "15", "16"},
                {"17", "18", "19", "20"},
                {"21", "22", "23", "24"}
            },
            new String [] {
                "One Column", "Two Column", "Three Column", "Forur Column"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }
        });
        jScrollPane1.setViewportView(content_Table);

        export_Button.setText("Export Table To Excel");
        export_Button.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                export_ButtonActionPerformed(evt);
            }
        });

        one_Lable.setText("One");

        two_Lable.setText("Two");

        three_Lable.setText("Three");

        four_Lable.setText("Four");

        print_Button.setText("Print");
        print_Button.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                print_ButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 908, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addGap(74, 74, 74)
                .addComponent(one_Lable)
                .addGap(82, 82, 82)
                .addComponent(two_Lable)
                .addGap(88, 88, 88)
                .addComponent(three_Lable)
                .addGap(92, 92, 92)
                .addComponent(four_Lable)
                .addContainerGap(482, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(741, Short.MAX_VALUE)
                .addComponent(print_Button)
                .addGap(112, 112, 112))
            .addGroup(layout.createSequentialGroup()
                .addGap(122, 122, 122)
                .addComponent(export_Button)
                .addContainerGap(649, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(23, 23, 23)
                .addComponent(export_Button)
                .addGap(39, 39, 39)
                .addComponent(print_Button)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(one_Lable)
                    .addComponent(two_Lable)
                    .addComponent(three_Lable)
                    .addComponent(four_Lable))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 137, Short.MAX_VALUE)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 123, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void export_ButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_export_ButtonActionPerformed
        // TODO add your handling code here:

        String fileName = "";
        JFileChooser fileChooser = new JFileChooser();
        fileChooser.setBounds(1, 1, 100, 100);

        switch (fileChooser.showSaveDialog(this)) {
            case JFileChooser.APPROVE_OPTION:

                fileName = fileChooser.getSelectedFile() + "";
                JOptionPane.showMessageDialog(this, "Selected: "
                        + fileChooser.getSelectedFile(),
                        "FCDemo",
                        JOptionPane.OK_OPTION);


                break;

            case JFileChooser.CANCEL_OPTION:
                JOptionPane.showMessageDialog(this, "Cancelled",
                        "FCDemo",
                        JOptionPane.OK_OPTION);
                break;

            case JFileChooser.ERROR_OPTION:
                JOptionPane.showMessageDialog(this, "Error",
                        "FCDemo",
                        JOptionPane.OK_OPTION);
        }


        one_Lable.setText(content_Table.getColumnName(0));

        two_Lable.setText(content_Table.getColumnCount() + "");

        javax.swing.table.DefaultTableModel defaultTableModel = (javax.swing.table.DefaultTableModel) content_Table.getModel();

        three_Lable.setText(defaultTableModel.getValueAt(0, 0).toString());



        four_Lable.setText(defaultTableModel.getRowCount() + "");





        int cellCount = content_Table.getColumnCount();
        int rowCount = defaultTableModel.getRowCount();


        HSSFWorkbook workbook = new HSSFWorkbook();
        HSSFSheet sheet = workbook.createSheet("Test Sheet");





        Row header = sheet.createRow(0);

        for (int i = 0; i < cellCount; i++) {

            sheet.setColumnWidth(i, 6000);
            header.createCell(i).setCellValue(content_Table.getColumnName(i));
        }



        for (int i = 0; i < rowCount; i++) {
            header = sheet.createRow(i + 1);

            for (int j = 0; j < cellCount; j++) {

                sheet.setColumnWidth(j, 6000);
                header.createCell(j).setCellValue(defaultTableModel.getValueAt(i, j) + "");


            }


        }





//        }


        try {
            FileOutputStream out =
                    new FileOutputStream(new File(fileName + ".xls"));
            workbook.write(out);
            out.close();


        } catch (Exception e) {
            e.printStackTrace();
        }


    }//GEN-LAST:event_export_ButtonActionPerformed

    private void print_ButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_print_ButtonActionPerformed
        try {
            // TODO add your handling code here:
            content_Table.print();
        } catch (PrinterException ex) {
            Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);
        }


    }//GEN-LAST:event_print_ButtonActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new Test().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTable content_Table;
    private javax.swing.JButton export_Button;
    private javax.swing.JLabel four_Lable;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel one_Lable;
    private javax.swing.JButton print_Button;
    private javax.swing.JLabel three_Lable;
    private javax.swing.JLabel two_Lable;
    // End of variables declaration//GEN-END:variables
}
